package service

import (
	"context"
	pb "shop-demo/api/shop/bff/v1"
)

func (s *ShopService) ListProduct(ctx context.Context, req *pb.ListProductReq) (*pb.ListProductReply, error) {
	products, err := s.puc.ListProduct(ctx, int(req.PageNum), int(req.PageSize))
	if err != nil {
		return nil, err
	}
	var replyProducts = make([]*pb.ListProductReply_Product, 0, len(products))
	for _, product := range products {
		replyProducts = append(replyProducts, &pb.ListProductReply_Product{
			Id:   product.Id,
			Num:  product.Num,
			Name: product.Name,
		})
	}
	return &pb.ListProductReply{Products: replyProducts}, nil
}
func (s *ShopService) GetProduct(ctx context.Context, req *pb.GetProductReq) (*pb.GetProductReply, error) {
	product, err := s.puc.GetProduct(ctx, req.Id)
	if err != nil {
		return nil, err
	}
	return &pb.GetProductReply{
		Id:   product.Id,
		Num:  product.Num,
		Name: product.Name,
	}, nil
}
